$(document).ready(function(){
});

function calcSchedule()
{
    var schedule_start_date_text = $("#schedule_start_date").val();
    var schedule_payment_start_date_text = $("#schedule_payment_start_date").val();
    var schedule_length = parseInt($("#schedule_length").val());
    var counter = 0;
    
    var tmpPrevMonth = new Date();
    var tmpNextMonth = new Date();
    
    if(schedule_start_date_text.length > 0 && schedule_payment_start_date_text.length > 0)
    {        
        var schedule_start_date = new Date(schedule_start_date_text);
        var schedule_payment_start_date = new Date(schedule_payment_start_date_text);
        var tmpFirstPaymentDate = new Date(schedule_payment_start_date);
        
        if(schedule_start_date > schedule_payment_start_date)
        {
            alert('"Эхний төлбөр хийх огноо" нь "Зээл олгох огноо" - с хойш байж болохгүй');
            $("#schedule_payment_start_date").val('');
            return;
        }
        
        // clear
        $("#schduleTbody").html('');
        
        for(i = 0; i < schedule_length; i++)        
        {
            
            if(i === 0)
            {
                tmpPrevMonth = new Date(schedule_payment_start_date);                
                schedule_payment_start_date.setMonth(schedule_payment_start_date.getMonth() + 1);
                tmpNextMonth = schedule_payment_start_date;                
                counter = i + 1;                
                initTableData(
                        counter, 
                        tmpPrevMonth.yyyymmdd()+'<input type="hidden" class="col-md-12 currency" name="schedule_date['+counter+']" value="'+tmpPrevMonth.yyyymmdd()+'"/>', 
                        getDifferenceDay(schedule_start_date, tmpFirstPaymentDate)+'<input type="hidden" class="col-md-12 currency" name="schedule_date_length['+counter+']" value="'+getDifferenceDay(schedule_start_date, tmpFirstPaymentDate)+'"/>', 
                        '<input type="text" class="col-md-12 currency" name="schedule_main_amount['+counter+']" />', 
                        '<input type="text" class="col-md-12 currency" name="schedule_interest_amount['+counter+']" />', 
                        '<input type="text" class="col-md-12 currency" name="schedule_total_amount['+counter+']" />', 
                        '<input type="text" class="col-md-12 currency" name="schedule_balance['+counter+']" />'
                );
            }else{
                tmpPrevMonth = new Date(tmpNextMonth);
                tmpNextMonth.setMonth(tmpNextMonth.getMonth() + 1);                
                counter = i + 1;
                initTableData(
                        counter, 
                        tmpPrevMonth.yyyymmdd()+'<input type="hidden" class="col-md-12 currency" name="schedule_date['+counter+']" value="'+tmpPrevMonth.yyyymmdd()+'"/>', 
                        getDifferenceDay(tmpPrevMonth, tmpNextMonth)+'<input type="hidden" class="col-md-12 currency" name="schedule_date_length['+counter+']" value="'+getDifferenceDay(tmpPrevMonth, tmpNextMonth)+'"/>', 
                        '<input type="text" class="col-md-12 currency" name="schedule_main_amount['+counter+']" />', 
                        '<input type="text" class="col-md-12 currency" name="schedule_interest_amount['+counter+']" />', 
                        '<input type="text" class="col-md-12 currency" name="schedule_total_amount['+counter+']" />', 
                        '<input type="text" class="col-md-12 currency" name="schedule_balance['+counter+']" />'
                );
            }
        }
        
    }
    
    $(".currency").keyup(function() {
        $(this).formatCurrency();
    });
}

function initTableData(col1, col2, col3, col4, col5, col6, col7)
{
    var html = "<tr><td>"+col1+"</td><td>"+col2+"</td><td>"+col3+"</td><td>"+col4+"</td><td>"+col5+"</td><td>"+col6+"</td><td>"+col7+"</td></tr>";
    var hiddenScheduleRowNum = parseInt($("#hiddenScheduleRowNum").val());
    
    if(hiddenScheduleRowNum === 0)
    {
        $("#schduleTbody").html(html);
    }else{
        $("#schduleTbody").append(html);
    }
    
    hiddenScheduleRowNum++;
    $("#hiddenScheduleRowNum").val(hiddenScheduleRowNum);
    
}

Date.prototype.yyyymmdd = function() {
   var yyyy = this.getFullYear().toString();
   var mm = (this.getMonth() + 1).toString(); // getMonth() is zero-based
   var dd  = this.getDate().toString();
   return yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+dd[0]); // padding
};

function getDifferenceDay(date1, date2)
{    
    if(date1 instanceof Date && date2 instanceof Date)
    {
        var timeDiff = Math.abs(date2.getTime() - date1.getTime());
        var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));     
        return diffDays;
    }else{
        return 0;
    }    
}

function changePaymentType()
{
    $("#schedule_payment_type_text").html($("#credit_payment_type").select2('data').text);
    $("#schedule_payment_type").val($("#credit_payment_type").select2('data').id);
}

function changeTotalAmount()
{
    $("#schedule_total_amount").html($("#credit_total_amount").val());
}


function changeInterest()
{
    $("#schedule_intereset").html($("#credit_interest_percent").val());
}

function changeTotalCurrency()
{
    $("#schedule_intereset").html($("#credit_currency_id").select2('data').text);
}